//
//  makeMoneyTableViewController.m
//  FastRunDemo
//
//  Created by 苟亚明 on 16/9/22.
//  Copyright © 2016年 苟亚明. All rights reserved.
//

#import "makeMoneyTableViewController.h"
#import "XMGLineLayout.h"
#import "XMGPhotoCell.h"
#import "meSelectView.h"
@interface makeMoneyTableViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
{
    UIPageControl *pageCtrl;
    UICollectionView *collectionView;
}

@end
static NSString * const XMGPhotoId = @"photo";
@implementation makeMoneyTableViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.titleTextAttributes =  @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:17]};
    
    UIImage *image = [UIImage imageNamed:@"transparent-1"];
    [self.navigationController.navigationBar setBackgroundImage:image
                                                  forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:image];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我要赚钱";
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"arrow_back" highImage:@"arrow_back" target:self action:@selector(leftBtnClick)];

    
    self.view.backgroundColor = [UIColor whiteColor];
    // 创建布局
    XMGLineLayout *layout = [[XMGLineLayout alloc] init];
    layout.itemSize = CGSizeMake(200, 250);

    // 创建CollectionView
    CGFloat collectionW = self.view.frame.size.width;
    
    CGRect frame = CGRectMake(0, 0, collectionW, GYMScreenH * 0.65);
    collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];
    
    collectionView.showsHorizontalScrollIndicator = NO;
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:collectionView.bounds];
    imgView.image = [UIImage imageNamed:@"makeMoney_backImg"];
    collectionView.backgroundView = imgView;
    
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [self.view addSubview:collectionView];
    
    pageCtrl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, collectionView.height - 34, GYMScreenW, 30)];  //创建UIPageControl，位置在屏幕最下方。
    pageCtrl.numberOfPages = 8;//总的图片页数
    pageCtrl.currentPage = 0; //当前页
    [pageCtrl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];  //用户点击UIPageControl的响应函数
    
    [self.view addSubview:pageCtrl];  //将UIPageControl添加到主界面上。
    
    meSelectView *selectView = [[[NSBundle mainBundle]loadNibNamed:@"meSelectView" owner:nil options:nil]lastObject];
    CGFloat selectViewY = CGRectGetMaxY(collectionView.frame);
    selectView.frame = CGRectMake(0, selectViewY, GYMScreenW, GYMScreenH - collectionView.height);
    [self.view addSubview:selectView];
    
    // 注册
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([XMGPhotoCell class]) bundle:nil] forCellWithReuseIdentifier:XMGPhotoId];
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}


- (void)pageTurn:(UIPageControl*)sender
{
    //令UIScrollView做出相应的滑动显示
    pageCtrl.currentPage = collectionView.contentOffset.x / 200;
    
}

- (void)leftBtnClick{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - collectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 8;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 20;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XMGPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:XMGPhotoId forIndexPath:indexPath];
    
    cell.imageName = @"makeMoney_selectPic";
    
    //    cell.backgroundColor = [UIColor orangeColor];
    //
    //    NSInteger tag = 10;
    //    UILabel *label = (UILabel *)[cell.contentView viewWithTag:tag];
    //    if (label == nil) {
    //        label = [[UILabel alloc] init];
    //        label.tag = tag;
    //        [cell.contentView addSubview:label];
    //    }
    //
    //    label.text = [NSString stringWithFormat:@"%zd", indexPath.item];
    //    [label sizeToFit];
    
    return cell;
}

#pragma mark - <UICollectionViewDelegate>
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"------%zd", indexPath.item);
}


@end
